<template>
  <el-form ref="deptform" :model="form" label-width="120px" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in EmployeesList" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeesList } from '@/api/employees'
import { addDepartments, editDepartmentsDetail, getDepartmentsDetail } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      // 添加不能出现同名code
      let testCode = this.originList.map(item => item.code)
      // 是否编辑
      if (this.isEdit) {
        // 编辑先排除自己
        testCode = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (testCode.includes(value)) {
        callback(new Error(`重复的${value},请重新输入`))
      } else {
        callback()
      }
    }
    const validName = (rule, value, callback) => {
      const pid = this.originList.find(item => item.id === this.id).pid
      let testName = this.originList.filter(item => item.pid === pid).map(item => item.name)
      if (this.isEdit) {
        testName = this.originList.filter(item => item.pid === pid).filter(item => item.id !== this.id).map(item => item.name)
      }
      if (testName.includes(value)) {
        callback(new Error(`重复的${value},请重新输入`))
      } else {
        callback()
      }
    }
    return {
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字符内容', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字符内容', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字符内容', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字符内容', trigger: 'blur' }
        ]
      },
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      EmployeesList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 确定
    hSubmit() {
      // 表单校验
      this.$refs.deptform.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 取消
    hCancel() {
      this.$emit('close')
    },
    // 重置
    reset() {
      this.$refs.deptform.resetFields()
    },
    async doEdit() {
      try {
        const res = await editDepartmentsDetail(this.form)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async doAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        // console.log(res)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        // 渲染页面操作在父组件需要父组件收尾
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async getDepartmentInform() {
      const res = await getDepartmentsDetail(this.id)
      // console.log(res)
      this.form = res.data
    },
    // 获取员工简单列表
    async  getList() {
      const res = await getEmployeesList()
      this.EmployeesList = res.data
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
